/**
 * Copyright (c) 2012 Nokia Corporation.
 * All rights reserved.
 *
 * For the applicable distribution terms see the license text file included in
 * the distribution.
 */

import QtQuick 1.1
import com.nokia.meego 1.0
import QtMobility.location 1.2
import "CommonFunctions.js" as Functions

Page {
    property string placeId: ""
    property Coordinate placeCoordinate: Coordinate { latitude: 0.0; longitude: 0.0; }

    /*!
      Updates the page content.
    */
    function handleRequestCompleted(requestId, result)
    {
        switch (requestId) {
        case "getPlaceDetails":
            // Update the content
            placeCoordinate.latitude = Functions.getDataFromJSON(result, "location.latitude");
            placeCoordinate.longitude = Functions.getDataFromJSON(result, "location.longitude");
            placeName.text = Functions.getDataFromJSON(result, "name");
            placeDesc.text = Functions.getDataFromJSON(result, "description");
            placePhone.text = Functions.getDataFromJSON(result, "phone");
            placeAddress.text = Functions.getDataFromJSON(result, "location.street");
            placeIcon.source = Functions.getDataFromJSON(result, "picture");
            var webSiteUrl = Functions.getDataFromJSON(result, "website");

            if (webSiteUrl != "") {
                placeWebsite.text = "<a href=\"" + webSiteUrl + "\">"
                        + webSiteUrl + "</a>";
            }
            break;
        default:
            break;
        }

        busyIndicator.opacity = 0;
    }

    /*!
      Process failed request.
    */
    function handleRequestFailed(requestId, reason)
    {
        busyIndicator.opacity = 0;
        console.log("PlacesDetailPage.qml: handleRequestFailed():",
                    requestId, reason);
    }

    /*!
      Hides detail page when route request completes.
     */
    function handleRouteRequestCompleted() {
        busyIndicator.opacity = 0;
        Functions.showMap();
    }

    Connections {
        target: fbConnect
        onRequestCompleted: handleRequestCompleted(requestId, result);
        onRequestFailed: handleRequestFailed(requestId, reason);
    }
    Connections {
        target: routeManager
        onRequestCompleted: handleRouteRequestCompleted();
    }

    onPlaceIdChanged: {
        busyIndicator.opacity = 1;
        fbConnect.request("getPlaceDetails", placeId);
    }

    Flickable {
        id: flickable
        anchors.fill: parent
        contentHeight: container.height + 40

        Item {
            id: container
            height: childrenRect.height

            anchors {
                top: parent.top
                left: parent.left
                right: parent.right
                topMargin: 10
                leftMargin: 20
                rightMargin: 20
            }

            Row {
                id: iconAndNameRow
                height: placeIcon.height

                anchors {
                    top: parent.top
                    left: parent.left
                    right: parent.right
                }

                spacing: 20

                Image {
                    id: placeIcon
                    width: height
                    height: 170
                    visible: source != ""
                    fillMode: Image.PreserveAspectFit
                    smooth: true
                }
                Label {
                    id: placeName
                    width: parent.width - placeIcon.width - 20
                    height: placeIcon.height
                    verticalAlignment: Text.AlignVCenter
                    wrapMode: Text.WordWrap
                }
            }
            Column {
                height: childrenRect.height

                anchors {
                    top: iconAndNameRow.bottom
                    left: parent.left
                    right: parent.right
                    topMargin: 10
                }

                spacing: 10

                Label {
                    id: placeDesc
                    width: parent.width
                    visible: text.length
                    wrapMode: Text.WordWrap
                    textFormat: Text.AutoText
                }
                Label {
                    id: placePhone
                    width: parent.width
                    visible: text.length
                }
                Label {
                    id: placeAddress
                    width: parent.width
                    visible: text.length
                    wrapMode: Text.WordWrap
                }
                Label {
                    id: placeWebsite
                    width: parent.width
                    visible: text.length
                    wrapMode: Text.WrapAnywhere
                    textFormat: Text.RichText
                    onLinkActivated: Qt.openUrlExternally(link);
                }
            }
        }
    }
    ScrollDecorator {
        flickableItem: flickable
    }

    BusyIndicator {
        id: busyIndicator
        anchors.centerIn: parent
        opacity: 0
        platformStyle: BusyIndicatorStyle { size: "large" }
        running: visible ? true : false;
        Behavior on opacity { NumberAnimation { duration: 300 } }
    }

    tools: ToolBarLayout {
        ToolIcon {
            iconId: "toolbar-back"
            onClicked: pageStack.pop();
        }
        ToolButton {
            enabled: placePhone.text != ""
            text: qsTr("Call");
            onClicked: {
                // Remove illegal characters from the phone number.
                var url = placePhone.text.replace(new RegExp("[^0-9\+]", "g"), "");
                Qt.openUrlExternally("tel:" + url);
            }
        }
        ToolButton {
            text: qsTr("Route");

            onClicked: {
                // Create route request
                busyIndicator.opacity = 1;
                main.map.drawRoute(placeCoordinate.latitude, placeCoordinate.longitude);
            }
        }
        Item { } // Placeholder for creating margin to the right
    }
}
